﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SmartSpace.Core;
using SmartSpace.Workflow;
using SsTool.Common;
using SmartSpace.Data;

namespace SsTool.Workflow.Windows
{
    /// <summary>
    /// Interaction logic for UpdateActivityWindow.xaml
    /// </summary>
    public partial class UpdateActivityWindow : Window
    {
        private ActivityMetadata _metadata;

        public UpdateActivityWindow(ActivityMetadata metadata)
        {
            InitializeComponent();

            _metadata = metadata;
            this.Loaded += new RoutedEventHandler(UpdateActivityWindow_Loaded);
        }

        void UpdateActivityWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = _metadata;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_metadata.Caption))
            {
                return;
            }

            //if (_metadata.ModuleView != null)
            //{
            //    _metadata.ModuleView.Caption = _metadata.Caption;
            //}
            DialogResult = true;
        }

        private void _ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlBtnAdd4Visit_Click(object sender, RoutedEventArgs e)
        {
            SelectRoleWindow window = new SelectRoleWindow();
            if (window.ShowDialog() == true)
            {
                foreach (Role newRole in window.SelectedRoles)
                {
                    bool isExist = false;

                    if (_metadata.Rights != null)
                    {
                        foreach (ActivityRight right in _metadata.Rights)
                        {
                            if (right.RoleId == newRole.Id)
                            {
                                isExist = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        _metadata.Rights = new List<ActivityRight>();
                    }

                    if (!isExist)
                    {
                        ActivityRight newRight = new ActivityRight();
                        newRight.Id = Guid.NewGuid();
                        newRight.Role = newRole;
                        newRight.RoleId = newRole.Id;
                        newRight.Activity = _metadata;
                        newRight.ActivityId = _metadata.Id;
                        newRight.IsAllowVisit = true;
                        newRight.IsAllowModify = true;
                        newRight.IsAllowRemove = true;

                        UpdateFeildRights(newRight, _metadata);

                        _metadata.Rights.Add(newRight);
                    }
                }

                this.DataContext = null;
                this.DataContext = _metadata;
            }
        }

        private void _ctlGridRights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
            if (selectedRight != null)
            {
                _ctlGridFieldRight.ItemsSource = selectedRight.FieldRights;
            }
            else
            {
                _ctlGridFieldRight.ItemsSource = null;
            }
        }

        private void UpdateFeildRights(ActivityRight right, ActivityMetadata activity)
        {
            if (right.FieldRights == null)
            {
                // 添加新的字段权限
                right.FieldRights = new List<FieldRight>();
                foreach (SmartSpace.Data.PropertyMetadata property in _metadata.Workflow.Entity.Properties)
                {
                    FieldRight field = new FieldRight();
                    field.Id = Guid.NewGuid();
                    field.ActivityRight = right;
                    field.ActivityRightId = right.Id;
                    field.ActivityId = activity.Id;
                    field.Property = property;
                    field.PropertyId = property.Id;

                    if (!property.Type.IsSystem)
                    {
                        field.IsList = true;
                        field.IsAllowVisit = true;
                        field.IsAllowModify = true;
                    }
                    else
                    {
                        if (_metadata.IsRoot)
                        {
                            // 流程第一步时，系统字段必须能填写
                            field.IsAllowModify = true;
                        }
                    }

                    right.FieldRights.Add(field);
                }
            }
        }

        #region 视图权限
        private void _ctlBtnAllowView_Click(object sender, RoutedEventArgs e)
        {
            ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
            if (selectedRight != null)
            {
                selectedRight.IsAllowVisit = !selectedRight.IsAllowVisit;

                _ctlGridRights.ItemsSource = null;
                _ctlGridRights.ItemsSource = _metadata.Rights;
                _ctlGridRights.SelectedItem = selectedRight;
            }
        }

        private void _ctlBtnAllowUpdate_Click(object sender, RoutedEventArgs e)
        {
            ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
            if (selectedRight != null)
            {
                selectedRight.IsAllowModify = !selectedRight.IsAllowModify;

                _ctlGridRights.ItemsSource = null;
                _ctlGridRights.ItemsSource = _metadata.Rights;
                _ctlGridRights.SelectedItem = selectedRight;
            }
        }

        private void _ctlBtnAllowDelete_Click(object sender, RoutedEventArgs e)
        {
            ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
            if (selectedRight != null)
            {
                selectedRight.IsAllowRemove = !selectedRight.IsAllowRemove;

                _ctlGridRights.ItemsSource = null;
                _ctlGridRights.ItemsSource = _metadata.Rights;
                _ctlGridRights.SelectedItem = selectedRight;
            }
        }
        #endregion

        #region 字段权限修改
        private void _ctlBtnIsList_Click(object sender, RoutedEventArgs e)
        {
            if (this._ctlGridFieldRight.SelectedItems != null && this._ctlGridFieldRight.SelectedItems.Count > 0)
            {
                for (int index = 0; index < this._ctlGridFieldRight.SelectedItems.Count; index++)
                {
                    FieldRight fr = this._ctlGridFieldRight.SelectedItems[index] as FieldRight;
                    fr.IsList = !fr.IsList;
                }

                ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
                _ctlGridFieldRight.ItemsSource = null;
                _ctlGridFieldRight.ItemsSource = selectedRight.FieldRights;
            }
        }

        private void _ctlBtnAllowVisitField_Click(object sender, RoutedEventArgs e)
        {
            if (this._ctlGridFieldRight.SelectedItems != null && this._ctlGridFieldRight.SelectedItems.Count > 0)
            {
                for (int index = 0; index < this._ctlGridFieldRight.SelectedItems.Count; index++)
                {
                    FieldRight fr = this._ctlGridFieldRight.SelectedItems[index] as FieldRight;
                    fr.IsAllowVisit = !fr.IsAllowVisit;

                    //if (!fr.IsAllowVisit)
                    //{
                    //    fr.IsAllowModify = false;
                    //}
                }

                ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
                _ctlGridFieldRight.ItemsSource = null;
                _ctlGridFieldRight.ItemsSource = selectedRight.FieldRights;
            }
        }

        private void _ctlBtnAllowModifyField_Click(object sender, RoutedEventArgs e)
        {
            if (this._ctlGridFieldRight.SelectedItems != null && this._ctlGridFieldRight.SelectedItems.Count > 0)
            {
                for (int index = 0; index < this._ctlGridFieldRight.SelectedItems.Count; index++)
                {
                    FieldRight fr = this._ctlGridFieldRight.SelectedItems[index] as FieldRight;
                    fr.IsAllowModify = !fr.IsAllowModify;

                    //if (!fr.Property.Type.IsSystem)
                    //{
                    //    if (fr.IsAllowModify)
                    //    {
                    //        fr.IsAllowVisit = true;
                    //    }
                    //}
                }
                
                ActivityRight selectedRight = _ctlGridRights.SelectedItem as ActivityRight;
                _ctlGridFieldRight.ItemsSource = null;
                _ctlGridFieldRight.ItemsSource = selectedRight.FieldRights;
            }
        }
        #endregion
    }
}
